import { IHttpGet, IHttpGetSync, IHttpPost } from "../core/http";
import { CLOUD_HOST, CLOUD_PORT, TOKEN } from "../tokens/const";
import { tryGet } from "../utils/tryGet";
export const httpPostAsync = (path: string, body: any) => {
    const host = tryGet(CLOUD_HOST)
    const port = tryGet(CLOUD_PORT)
    let url: string = path;
    if (!url.startsWith(`http`)) {
        url = `http://${host}:${port}/autojs/core/autox/${path}`
    }
    return http.postJson(url, body, {
        timeout: 5 * 1000
    })
}
export const httpGetAsync = (path: string, params?: any) => {
    const host = tryGet(CLOUD_HOST)
    const port = tryGet(CLOUD_PORT)
    const extraParams = Object.keys(params || {}).map(key => {
        return `${key}=${params[key]}`
    }).join('&');
    let url: string = path;
    if (!url.startsWith(`http`)) {
        url = `http://${host}:${port}/autojs/core/autox/${path}?${extraParams}`
    }
    return http.get(url, {
        timeout: 5 * 1000
    })
}
export const httpPost: IHttpPost = (path, body) => {
    return new Promise((resolve, reject) => {
        const host = tryGet(CLOUD_HOST)
        const port = tryGet(CLOUD_PORT)
        const token = tryGet(TOKEN)
        let url: string = path;
        if (!url.startsWith(`http`)) {
            url = `http://${host}:${port}/autojs/core/autox/${path}`
        }
        http.post(url, body, {
            timeout: 5 * 1000,
            contentType: 'application/json',
            headers: {
                Authorization: `Autox ${token}`
            }
        }, (res) => {
            if (res.statusCode !== 200) {
                return reject(new Error(`请求失败`));
            }
            if (res.headers["Content-Type"].includes('text/html')) {
                return reject(new Error(`请求失败ContentType类型有误`));
            }
            const resData: any = res.body.json();
            const { success, message, data } = resData;
            if (success) {
                resolve(data)
            } else {
                reject(new Error(message))
            }
        })
    })
}
export const httpGet: IHttpGet = (path, params) => {
    return new Promise((resolve, reject) => {
        const host = tryGet(CLOUD_HOST)
        const port = tryGet(CLOUD_PORT)
        const token = tryGet(TOKEN)
        const extraParams = Object.keys(params || {}).map(key => {
            return `${key}=${params[key]}`
        }).join('&');
        let url: string = path;
        if (!url.startsWith(`http`)) {
            url = `http://${host}:${port}/autojs/core/autox/${path}?${extraParams}`
        }
        http.get(url, {
            timeout: 5 * 1000,
            headers: {
                Authorization: `Autox ${token}`
            }
        }, (res) => {
            if (res.statusCode !== 200) {
                return reject(new Error(`请求失败`));
            }
            if (res.headers["Content-Type"].includes('text/html')) {
                return reject(new Error(`请求失败ContentType类型有误`));
            }
            const resData: any = res.body.json();
            const { success, message, data } = resData;
            if (success) {
                resolve(data)
            } else {
                reject(new Error(message))
            }
        })
    })
}

export const httpGetSync2: IHttpGetSync = (path, params) => {
    const host = tryGet(CLOUD_HOST)
    const port = tryGet(CLOUD_PORT)
    const token = tryGet(TOKEN)
    const extraParams = Object.keys(params || {}).map(key => {
        return `${key}=${params[key]}`
    }).join('&');
    let url: string = path;
    if (!url.startsWith(`http`)) {
        url = `http://${host}:${port}/autojs/core/autox/${path}?${extraParams}`
    }
    const res = http.get(url, {
        timeout: 5 * 1000,
        headers: {
            Authorization: `Autox ${token}`
        }
    })
    if (res.statusCode !== 200) {
        throw new Error(`请求失败`)
    }
    if (res.headers["Content-Type"].includes('text/html')) {
        throw new Error(`请求失败ContentType类型有误`)
    }
    const resData: any = res.body.json();
    const { success, message, data } = resData;
    if (success) {
        return data;
    } else {
        throw new Error(message)
    }
}